// pages/editAddress/editAddress.js
let util = require('../../utils/util');
const api = require('../../utils/api');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: true, //是否设置为默认地址
    regionObj: [],
    region: [],
    curReg: [0, 0, 0], //当前地区索引
    provice: "",
    city: "",
    area: "",
    errTip: false //信息不合法时的提示语

  },
  onCheck() {
    this.setData({
      checked: !this.data.checked
    })
  },
  onDetailAddress(e) {
    this.setData({
      detailAddress: e.detail.value
    })
  },
  checkInfo(e){
    let regExp = /^1[3-9]\d{9}$/;
    if(!regExp.test(e.detail.value)){
      this.setData({
        errTip: true
      })
    }
  },
  trueInfo(e) {
    this.setData({
      errTip: false
    })
  },
  //省份城市改变列事件
  bindPickerColumnChange(e) {
    let that = this;
    let curColumn = e.detail.column;
    let curIdx = e.detail.value;
    if (curColumn == 0) {
      let cityId = that.data.regionObj[0][curIdx].id;
      that.setData({
        curReg: [curIdx, 0, 0]
      })
      util.myRequest(api.regionList, {
          parentId: cityId
        },
        res => {
          let newArr = [];
          res.data.data.forEach(function (item, index) {
            newArr.push(item.name);
          })
          that.setData({
            ['regionObj[1]']: res.data.data,
            ['region[1]']: newArr
          })
          let areaId = res.data.data[0].id;
          util.myRequest(api.regionList, {
              parentId: areaId
            },
            res => {
              let newArr = [];
              res.data.data.forEach(function (item, index) {
                newArr.push(item.name);
              })
              that.setData({
                ['regionObj[2]']: res.data.data,
                ['region[2]']: newArr
              })
            }
          )
        }
      )
    } else if (curColumn == 1) {
      let areaId = that.data.regionObj[1][curIdx].id;
      that.setData({
        ['curReg[1]']: curIdx,
        ['curReg[2]']: 0
      })
      util.myRequest(api.regionList, {
          parentId: areaId
        },
        res => {
          let newArr = [];
          res.data.data.forEach(function (item, index) {
            newArr.push(item.name)
          })
          that.setData({
            ['regionObj[2]']: res.data.data,
            ['region[2]']: newArr
          })
        }
      )
    } else {
      let area = that.data.regionObj[1][curIdx];
    }
  },
  // 省份城市改变事件
  bindPickerChange(e) {
    let that = this;
    that.setData({
      provice: that.data.region[0][e.detail.value[0]],
      city: that.data.region[1][e.detail.value[1]],
      area: that.data.region[2][e.detail.value[2]]
    })
  },
  //初始地区数据
  onLoad() {
    let that = this;
    util.myRequest(api.regionList, {
        parentId: 1
      },
      res => {
        that.setData({
          ['regionObj[0]']: res.data.data
        });
        let newArr = [];
        res.data.data.forEach(function (item, index) {
          newArr.push(item.name);
        });
        that.setData({
          ['region[0]']: newArr
        })
        let cityId = res.data.data[0].id;
        util.myRequest(api.regionList, {
            parentId: cityId
          },
          res => {
            that.setData({
              ['regionObj[1]']: res.data.data
            });
            let newArr = [];
            res.data.data.forEach(function (item, index) {
              newArr.push(item.name);
            })
            that.setData({
              ['region[1]']: newArr
            })
            let areaId = res.data.data[0].id;
            util.myRequest(api.regionList, {
                parentId: areaId
              },
              res => {
                let newArr = [];
                res.data.data.forEach(function (item, index) {
                    newArr.push(item.name);
                  }),
                  that.setData({
                    ['regionObj[2]']: res.data.data,
                    ['region[2]']: newArr
                  })
                that.setData({
                  provice: that.data.region[0][0],
                  city: that.data.region[1][0],
                  area: that.data.region[2][0]
                })
              }
            )
          }
        )
      }
    )
  },
  //保存
  onSave(e) {
    let value = e.detail.value;
    console.log(e.detail.value)
    let that = this;
    let proId = that.data.regionObj[0][that.data.curReg[0]].id;
    let cityId = that.data.regionObj[1][that.data.curReg[1]].id;
    let disId = that.data.regionObj[2][that.data.curReg[2]].id;
    let name = value.name;
    let telphone = value.telphone;
    let address = value.detailAddress;
    let isDefault = that.data.checked;
    if (!that.data.errTip && name && telphone && proId && cityId && disId && address){
      util.myRequest(api.addressSave,
        {
          //id,name,mobile,province_id, city_id,district_id,address,is_default
          id: {},
          name: name,
          mobile: telphone,
          province_id: proId,
          city_id: cityId,
          district_id: disId,
          address: address,
          is_default: isDefault
        },
        res => {
          console.log(res)
          wx.showToast({
            title: '保存成功',
          })
        },
        "POST"
      )
    } else {
      wx.showModal({
        title: "保存失败",
        content: "请将信息填写完整"
      })
    }
  }
})